{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>Phase Kickback</h2>\n",
    "\n",
    "We will observe a very interesting quantum affect here.\n",
    "\n",
    "We apply a Controlled-NOT operator, but the controller qubit will be affected (!)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 1</h3>\n",
    "\n",
    "Create a quantum circuit with two qubits.\n",
    "\n",
    "Set the state of the first qubit to $ \\ket{0} $.\n",
    "\n",
    "Set the state of the second qubit to $ \\ket{1} $.\n",
    "\n",
    "Apply Hadamard to both qubits.\n",
    "\n",
    "Apply CNOT operator, where the controller qubit is the first qubit and the target qubit is the second qubit.\n",
    "\n",
    "Apply Hadamard to both qubits.\n",
    "\n",
    "Measure the outcomes.\n",
    "\n",
    "We start in quantum state $ \\ket{01} $. What is the outcome?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "#\n",
    "# your code is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B64_Phase_Kickback_Solutions.ipynb#task1\">click for our solution</a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> The affect of CNOT </h3>\n",
    "\n",
    "The quantum state of the first qubit before CNOT:\n",
    "\n",
    "$$\n",
    "    \\ket{0} \\xrightarrow{H} \\frac{1}{\\sqrt{2}} \\ket{0} + \\frac{1}{\\sqrt{2}} \\ket{1}.\n",
    "$$\n",
    "\n",
    "The quantum state of the second qubit before CNOT:\n",
    "\n",
    "$$\n",
    "    \\ket{1} \\xrightarrow{H} \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}.\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The quantum state of the composite system:\n",
    "\n",
    "$$\n",
    "   \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} + \\frac{1}{\\sqrt{2}} \\ket{1} } \n",
    "   \\otimes\n",
    "   \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}  }\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "CNOT affects when the first qubit has the value 1.\n",
    "\n",
    "Let's rewrite the composite state as below to explicitly represent the effect of CNOT.\n",
    "\n",
    "$$\n",
    "     \\frac{1}{\\sqrt{2}} \\ket{0} \\otimes \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}  }\n",
    "     +\n",
    "     \\frac{1}{\\sqrt{2}} \\ket{1} \\otimes \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}  }\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "CNOT flips the state of the second qubit.\n",
    "\n",
    "After CNOT, we have:\n",
    "\n",
    "$$\n",
    "     \\frac{1}{\\sqrt{2}} \\ket{0} \\otimes \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}  }\n",
    "     +\n",
    "     \\frac{1}{\\sqrt{2}} \\ket{1} \\otimes \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{1} - \\frac{1}{\\sqrt{2}} \\ket{0}  }\n",
    "$$\n",
    "\n",
    "Remark that $\\ket{0}$ and $ \\ket{1} $ are swapped in the second qubit.\n",
    "\n",
    "If we write the quantum state of the second qubit as before, the sign of $ \\ket{1} $ in the first qubit should be flipped.\n",
    "\n",
    "Thus the last equation can be equivalently written as follows:\n",
    "\n",
    "$$\n",
    "     \\frac{1}{\\sqrt{2}} \\ket{0} \\otimes \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}  }\n",
    "     -\n",
    "     \\frac{1}{\\sqrt{2}} \\ket{1} \\otimes \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}  }\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before CNOT operator, the sign of $ \\ket{1} $ in the first qubit is positive. \n",
    "    \n",
    "After CNOT operator, its sign changes to negative.\n",
    "\n",
    "This is called <b>phase kickback</b>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> After CNOT </h3>\n",
    "\n",
    "It is easy to see from the last expression, the quantum states of the qubits are separable (no correlation):\n",
    "\n",
    "$$\n",
    "   \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1} } \n",
    "   \\otimes\n",
    "   \\mypar{ \\frac{1}{\\sqrt{2}} \\ket{0} - \\frac{1}{\\sqrt{2}} \\ket{1}  }\n",
    "$$\n",
    "\n",
    "If we apply Hadamard to each qubit, both qubits evolve to state $ \\ket{1} $.\n",
    "\n",
    "The final state is $ \\ket{11} $."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Task 2 </h3>\n",
    "\n",
    "Create a curcuit with 7 qubits.\n",
    "\n",
    "Set the states of the first six qubits to $ \\ket{0} $.\n",
    "\n",
    "Set the state of the last qubit to $ \\ket{1} $.\n",
    "\n",
    "Apply Hadamard operators to all qubits.\n",
    "\n",
    "Apply CNOT operator (first-qubit,last-qubit) \n",
    "<br>\n",
    "Apply CNOT operator (fourth-qubit,last-qubit)\n",
    "<br>\n",
    "Apply CNOT operator (fifth-qubit,last-qubit)\n",
    "\n",
    "Apply Hadamard operators to all qubits.\n",
    "\n",
    "Measure all qubits. \n",
    "\n",
    "For each CNOT operator, do we have phase-kickback affect?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import all necessary objects and methods for quantum circuits\n",
    "from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute, Aer\n",
    "from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer\n",
    "\n",
    "#\n",
    "# your code is here\n",
    "#\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"..\\bronze-solutions\\B64_Phase_Kickback_Solutions.ipynb#task2\">click for our solution</a>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
